﻿@{
    ViewBag.Title = "选择地址";
    Layout = "~/Views/Shared/_layoutPersonal.cshtml";
}
@Styles.Render("~/Content/css/chooseAddress")

<article class="main">
    <nav class="mod_nav">
        <div class="lay_left">
            <a class="nav_back" href='@Url.Content("~/")Home/Index'><img src="/images/TurnBackNormal.png"></a>
            <span>选择地址</span>
        </div>
    </nav>

    <section class="mod_select">
        <a class="mySelect" onclick="showCitySelection()">
            <span id="city">深圳</span><span>&nbsp;<img src="~/images/item_collasped.png" height="13" width="13" /></span>
        </a>
        <input id="inputAddress" class="input_address" type="text" placeholder="请输入详细地址">
    </section>

    <section class="mod_address top location">
        <ul>
            <li>
                <a class="btn_vending" style="padding-top:1rem;" onclick="clickLocation('@Url.Content("~/Home/Index")')">
                    <span class=" address" style="color:red; text-align:center;">
                        <img src="/images/poi_icon.png" height="15" width="15" style=" vertical-align:middle;" />&nbsp;定位到当前位置
                    </span>
                </a>
            </li>
        </ul>
    </section>

    <section class="mod_address poiPoint addressListCon">
        <div class="goods_window">
            <div class="goods_doc">
                <ul id="addressListInfo"></ul>
            </div>
        </div>
    </section>

    <section class="mod_title">历史位置</section>

    <section class="mod_address historyPoint"></section>

    <section class="OrderGray" onclick="cancelSelectCity()"></section>

    <section class="OrderTimeChoose">
        <div class="OrderTimeBtn">
            <a onclick="cancelSelectCity()">取消</a>
            <a id="btnSelectCity" onclick="setSelectCity()">确定</a>
        </div>
        <div class="OrderTimeCon">
            <div class="OrderTimeLine"></div>
            <ul class="OrderTimeFir"></ul>
        </div>
    </section>
</article>

<script>
	var scrollBegin = 0;
    $(function () {
        //当前浏览器窗口高度 考虑到header、地址搜索栏和定位的高度
        var currentWindowHeight = $(window).height() - 200; //由于 .vender 的高度变化了
        //当前容器确定高度
        $(".addressListCon").css("height", currentWindowHeight + "px");
        var goodsWindow = $(".goods_window");
        var goodsDoc = $(".goods_doc");
        iscroll(goodsWindow, goodsDoc, function () {
            //此处为加载数据处理，因为这里是数据是提前加载好，所以不用做处理
        });
    });
    //滚动处理
    function iscroll(scrollWindow, scrollDoc, scrollCallBack) {
        $(scrollWindow).scroll(function () {
            var scrollTop = $(this).scrollTop();
            var windowHeight = $(this).height();
            var docHeight = $(scrollDoc).height();
            if (scrollTop >= (docHeight - windowHeight)) {
                scrollBegin++;
                if (scrollCallBack) {
                    scrollCallBack();
                };
            } else {
                
            }
        });
    }
    window.onload = function () {
        adjustHistoryPoint();
        getServiceCity();
    }

    function clickLocation(url) {
        AddressMgr.useWXAddr();
        window.location.href = url;
    }

    function adjustHistoryPoint() {
        var isLog = UserInfoMgr.isLogIn();
        if (isLog) {
            showHistoryPoint();
        } else {
            hideHistoryPoint();
        }
    }

    function hideHistoryPoint() {
        $(".mod_title").attr("style", "display:none");
    }

    function showHistoryPoint() {
        var historyPointList = UserInfoMgr.getHistoryPoiList();
        if (null == historyPointList) {
            getHistoryPoint();
        } else {
            var myObj = eval(historyPointList);
            if (myObj.length > 0) {
                showHistoryPointList(historyPointList);
            } else {
                getHistoryPoint();
            }
        }
    }

    function showHistoryPointList(myObj) {
        $(".mod_title").attr("style", "display:block");
        $(".historyPoint").attr("style", "display:block");
        $("ul.addressListInfo li").each(function () {
            $(this).remove();
        });
        for (var i = 0; i < myObj.length; i++) {
            var $li = $('<li><a class=\"btn_vending\" onclick=\"choosePointAddress(\'' + myObj[i].name + '\',\'' + myObj[i].address + '\',' + myObj[i].gps.latitude + ',' + myObj[i].gps.longitude + ')\" style=\"padding-top:1rem;\"><span class=\"address\">' + myObj[i].address + '</span></a></li>');
            $(".historyPoint").append($li);
        }
    }
    
    function getHistoryPoint() {
        var sessionId = UserInfoMgr.getSessionId();
        var historyPointRequestJson = {
            "session_id": sessionId
        }
        var command = "history_poi";
        doAjax(historyPointRequestJson, command, function (result) {
            if (result.result == 0) {
                UserInfoMgr.setHistoryPoiList(result.address_info);
                showHistoryPoint();
            } else {
                hideHistoryPoint();
            }
        });
    }

    $(document).ready(function () {
        $("#inputAddress").bind('input propertychange', function () {
            var keyword = $("#inputAddress").val();
            if (keyword == "") {
                $(".location").attr("style", "display:block");
                getHistoryPoint();
                clearPOIList();
            } else {
                $(".location").attr("style", "display:none");
                $(".mod_title").attr("style", "display:none");
                $(".historyPoint").attr("style", "display:none");
                var city = $("#city").html();
                searchPoiByKeyword(keyword, city);
            }
        });
    });

    function clearPOIList() {
        $("ul#addressListInfo li").each(function () {
            $(this).remove();
        });
    }

    function searchPoiByKeyword(keyword, city) {
        url = 'http://api.map.baidu.com/place/v2/search?query=' + encodeURIComponent(keyword) + '&scope=2&region=' + encodeURIComponent(city) + '&page_size=10&page_num=0&output=json&ak=UC03PvVN1AEqsb6csSzY6vIF&callback=?';
        $.getJSON(url, function (msg) {
            if (msg.message == "ok") {
                clearPOIList();
                var myObj = eval(msg.results);
                for (var i = 0; i < myObj.length; i++) {
                    var $li = $('<li><a class=\"btn_vending\" onclick=\"choosePointAddress(\'' + myObj[i].name + '\',\'' + myObj[i].address + '\',' + myObj[i].location.lat + ',' + myObj[i].location.lng + ')\"><span class=\"address1\">' + myObj[i].name + '</span><span class=\"address2\">' + myObj[i].address + '</span></a></li>');
                    $("#addressListInfo").append($li);
                }
            } else {
                var $li = $('<li><a class=\"btn_vending\" style=\"padding-top:1rem;\"><span class=\"address\">请重新输入</span></a></li>');
                $("#addressListInfo").append($li);
            }
        });
    }

    function choosePointAddress(name, addr, lat, log) {
        AddressMgr.setCurAddr(name, addr, Number(lat), Number(log));
        window.location.href = '@Url.Content("~/Home/Index")';
    }

    function setCityList(cityList) {
        $("ul.OrderTimeFir li").each(function () {
            $(this).remove();
        });
        var $li = $('<li>&nbsp;</li>');
        $(".OrderTimeFir").append($li);
        for (var i = 0; i < cityList.length; i++) {
            if (i == 0) {
                var $li = $('<li class=\"OrderTimeFirFocus\">' + cityList[i] + '</li>');
                $(".OrderTimeFir").append($li);
            }
            else {
                var $li = $('<li >' + cityList[i] + '</li>');
                $(".OrderTimeFir").append($li);
            }
        }
    }
    //获取开通服务的城市
    function getServiceCity() {
        var cityList = AddressMgr.getCityList();
        if (null == cityList) {
            var command = "service_area";
            doAjax(null, command, function (result) {
                if (result.result == 0) {
                    AddressMgr.setCityList(result.city);
                    cityList = AddressMgr.getCityList();
                    setCityList(cityList);
                }
            });
        } else {
            setCityList(cityList);
        }
    }

    function showCitySelection() {
        $('.OrderGray').show();
        $('.OrderTimeChoose').show();
        $('body').css('overflow', 'hidden');
    }

    function setSelectCity() {
        $('.OrderGray').hide();
        $('.OrderTimeChoose').hide();
        $('body').css('overflow', 'scroll');
        $('#city').text($('.OrderTimeFir').children('.OrderTimeFirFocus').text());
    }

    function cancelSelectCity() {
        $('.OrderGray').hide();
        $('.OrderTimeChoose').hide();
        $('body').css('overflow', 'scroll');
    }
</script>
